package com.zkh.myutils.io.excel.data;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import com.zkh.myutils.utils.Utils;

/**
 * sheet数据对象
 * @author zkh
 *
 */
public class SheetData {
	//行数
	private int rows;
	//列数
	private int cols;
	//数据容器
	private List<CellData[]> dataList;
	
	public SheetData(int rows, int cols) {
		this.rows = rows;
		this.cols = cols;
		this.dataList = new ArrayList<>();
	}
	
	/**
	 * 遍历数据，自定义处理方式
	 * @param consumer 自定义处理逻辑
	 */
	public void eachRow(BiConsumer<CellData[], Integer> consumer) {
		//遍历行数据
		for(int i=0; i<this.rows; i++) {
			consumer.accept(dataList.get(i), i+1);
		}
	}
	
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getCols() {
		return cols;
	}
	public void setCols(int cols) {
		this.cols = cols;
	}
	public List<CellData[]> getDataList() {
		return dataList;
	}
	/**
	 * 获取指定行数据
	 * @param row 指定行号
	 * @return
	 */
	public CellData[] getRowData(int row) {
		return dataList.get(row-1);
	}
	public void setDataList(List<CellData[]> dataList) {
		this.dataList = dataList;
	}
	/**
	 * 添加一行数据
	 * @param data
	 */
	public void putRowData(CellData... data) {
		this.dataList.add(data);
	}
	
	/**
	 * 添加一行空数据
	 */
	public void putEmptyRow() {
		this.dataList.add(new CellData[cols]);
	}

	@Override
	public String toString() {
		//组装结果
		StringBuilder sb = new StringBuilder("SheetData => rows=" + rows + ", cols=" + cols + ", dataList：\r\n");
		//遍历
		dataList.forEach(e->{
			sb.append("\t").append(Utils.join(", ", e)).append("\r\n");
		});
		//返回
		return sb.toString();
	}
}
